home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / scherz programme / pointereyesv4.3&3.11 / v1.1.source / pointereyesv1.1.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  16KB  |  524 lines

  1. /*  PointerEyes Copyright © 1994 John Hughes.  All Rights Reserved.
  2.     Coded by:  John Hughes
  3.     E-Mail:    johughes@heartland.bradley.edu
  4.     Compiler:  SAS/C 6.51
  5.     Date:      July 12,1994
  6.     Machine:   A4000 68EC030 / 25 Mhz  6MB RAM
  7.  
  8.  
  9.     PointerEyes was written in a modular fashion, but
  10.     for the simplicity of the program, and since
  11.     there are so few variables, most of the functions
  12.     do not take parameters, and most variables are global.
  13.     However in most cases you would want to make as many
  14.     local variables as possible so that they are not
  15.     accidently altered outside of there function.
  16.  
  17.     This source code may only be distributed unmodified and
  18.     accompanied by the entire PointerEyes archive.
  19.  
  20.     This source code is 100% compileable with SAS/C and GCC.
  21.     And it should work with DICEC.  Please see the docs.
  22.     The GCC support was added by Gunther Nikl.
  23. */
  24.  
  25. #include <exec/execbase.h>
  26. #include <math.h>
  27. #include <stdlib.h>                /* abs() srand() rand() */
  28. #include <time.h>                  /* time() */
  29. #include <dos/notify.h>            /* StartNotify() */
  30. #include <exec/memory.h>           /* MEMF_CLEAR */
  31. #include <libraries/gadtools.h>    /* GTMN_FrontPen */
  32.  
  33. #if defined(__SASC) || defined(LATTICE)
  34. #include <proto/dos.h>
  35. #include <proto/exec.h>
  36. #include <proto/gadtools.h>
  37. #include <proto/intuition.h>
  38. #else
  39. #if defined(__GNUC__) && defined(__OPTIMIZE__)
  40. #include <inline/dos.h>
  41. #include <inline/exec.h>
  42. #include <inline/gadtools.h>
  43. #include <inline/intuition.h>
  44. #else
  45. #include <clib/dos_protos.h>
  46. #include <clib/exec_protos.h>
  47. #include <clib/gadtools_protos.h>
  48. #include <clib/intuition_protos.h>
  49. #endif
  50. #endif
  51.  
  52. #include "PointerEyes.Images"
  53.  
  54. #define DEFAULTXOFFSET 520
  55.  
  56. int    OldPriority, CurCoOrd, CurrentImage, SameImage, PrevCoOrd;
  57. UWORD  YOffset=0,XOffset=DEFAULTXOFFSET;
  58. struct Window *EyesWindow;
  59. struct RastPort *Rast;
  60. UBYTE  ver[]="\0$VER: PointerEyes 1.1 (7/12/94)  John Hughes  johughes@heartland.bradley.edu\0";
  61.  
  62. /*****StartNotify vars/functions********************************************/
  63.  
  64. struct NotifyRequest *notifyrequest;
  65. LONG reqnotifysignum=NULL;
  66. ULONG startnotsigflag,sigrcvd;
  67.  
  68. BOOL CreateReqNotify(UBYTE *printerfilespath);
  69. void RemoveReqNotify(void);
  70.  
  71. /*****Misc prototypes********************************************************/
  72.  
  73. int  GetImageNumber(void);
  74. void ShowImage(void);
  75. void BlinkEyes(void);
  76. void LookAround(void);
  77. void CloseEyes(void);
  78. void GoToSleep(void);
  79. void ShowRequester(STRPTR RequesterText);
  80.  
  81. /***************************************************************************/
  82.  
  83. main ()
  84. {
  85.   struct Screen *scr;
  86.   struct DrawInfo *dr_info;
  87.   struct ExecBase *ExecBase;
  88.   BOOL   LOOP,watch,ToggleLockStat=FALSE;
  89.   APTR *EyesVisualInfo;
  90.   struct Menu *EyesMenu;
  91.   struct NewMenu EyesNewMenu[]={
  92.       NM_TITLE, "Project",          0, 0,       0, 0,
  93.       NM_ITEM,  "Window Locked",  "L", CHECKIT|MENUTOGGLE|CHECKED, 0, 0,
  94.     IM_ITEM,  &RD,              "A", 0,       0, 0,
  95.       NM_ITEM, "Quit",            "Q", 0,       0, 0,
  96.       NM_END,   NULL,               0, 0,       0, 0 };
  97.   UWORD menuNum,itemNum;
  98.   struct MenuItem *menuitemaddress;
  99.   struct IntuiMessage *MyMessage;
  100.   UWORD  UseDragBar=0;
  101.   BOOL IsFirstTimeThrough=TRUE;
  102.   BPTR filehandle;
  103.   
  104.   PrevCoOrd = 0;
  105.  
  106.   srand(time(NULL));
  107.  
  108.   if(ExecBase=(struct ExecBase *)OpenLibrary("exec.library",37))
  109.   {
  110.     CloseLibrary((struct Library *) ExecBase);
  111.  
  112.     /* Lets watch the prefs drawer */
  113.     CreateReqNotify("env:sys");
  114.  
  115.     LOOP=TRUE;
  116.  
  117.     while (LOOP)  /** This is the main loop **/
  118.     {
  119.       /***************************************************/
  120.       /* Get the Y location of where to place the window */
  121.       /***************************************************/
  122.       if (UseDragBar || IsFirstTimeThrough)
  123.       {
  124.         if (scr=LockPubScreen(NULL))
  125.         {
  126.           if (dr_info=GetScreenDrawInfo(scr))
  127.           {
  128.             YOffset=(dr_info->dri_Font->tf_YSize+2-8)/2;
  129.             FreeScreenDrawInfo(scr,dr_info);
  130.           }
  131.           UnlockPubScreen(NULL,scr);
  132.         }
  133.       }
  134.       IsFirstTimeThrough=FALSE;
  135.  
  136.       if (( EyesWindow = OpenWindowTags( NULL,
  137.         WA_Left,     XOffset,
  138.         WA_Top,    YOffset,
  139.         WA_Width,  40,
  140.         WA_Height, 8,
  141.         WA_IDCMP,    IDCMP_MENUPICK,
  142.         WA_Flags, UseDragBar | WFLG_BORDERLESS | WFLG_NEWLOOKMENUS,
  143.         WA_Title,    "",
  144.         TAG_DONE )))
  145.       {
  146.         /* Set up menus */
  147.         if (EyesVisualInfo=GetVisualInfo(EyesWindow->WScreen,TAG_DONE))
  148.         {
  149.           if (EyesMenu = CreateMenus( EyesNewMenu, GTMN_FrontPen, 0L, TAG_DONE ))
  150.           {
  151.             if (UseDragBar)     /* If the window is unlcoked then Remove the check mark on the Lock menu item */
  152.               EyesMenu->FirstItem->Flags ^= CHECKED;
  153.             if (LayoutMenus( EyesMenu, EyesVisualInfo, TAG_DONE ))
  154.             {
  155.               if (SetMenuStrip( EyesWindow, EyesMenu ))
  156.               {
  157.                 GT_RefreshWindow( EyesWindow, NULL );  /* This line may not be needed */
  158.  
  159.                 /* Get rasterport to draw images */
  160.                 Rast=EyesWindow->RPort;
  161.  
  162.                 /* Set the task priority lower */
  163.                 OldPriority = SetTaskPri(FindTask(NULL),-2);
  164.  
  165.                 watch=TRUE;
  166.  
  167.                 while (watch)
  168.                 {
  169.                   /* If the window is not behind all other windows, and it is locked, then Send it to the Back! */
  170.                   if ((!UseDragBar) && (EyesWindow->WLayer->back->Window!=0)) /*Send the window to the back if we are in locked mode */
  171.                     WindowToBack(EyesWindow);
  172.  
  173.                   sigrcvd = SetSignal(0L,0L);
  174.  
  175.                   /* Preferences were modified! */
  176.                   if (sigrcvd & startnotsigflag)
  177.                   {
  178.                     watch=FALSE;
  179.                     SetSignal(0L,startnotsigflag);
  180.                   }
  181.  
  182.                   /* Quit if CTRL-C was recieved */
  183.                   if (sigrcvd & SIGBREAKF_CTRL_C)
  184.                   {
  185.                     watch=FALSE;
  186.                     LOOP=FALSE;
  187.                   }
  188.  
  189.                   /* Process IDCMP events */
  190.                   while (MyMessage = GT_GetIMsg(EyesWindow->UserPort))
  191.                   {
  192.                     switch(MyMessage->Class)
  193.                     {
  194.                       case    IDCMP_MENUPICK:
  195.                         while( MyMessage->Code != MENUNULL ) {
  196.                           menuitemaddress = ItemAddress( EyesMenu, MyMessage->Code );
  197.                           menuNum=MENUNUM(MyMessage->Code);
  198.                           itemNum=ITEMNUM(MyMessage->Code);
  199.                           if (menuNum==0)
  200.                             switch(itemNum)
  201.                             {
  202.                               case 0:   /* Un/Locked */
  203.                                 /* If the 'Lock Window' menu checkmark has been changed then change the drag bar flag. */
  204.                                 /* Pseudo Code: if ((window is locked and there is a check mark)
  205.                                                 or (window is unlocked and there isn't a check mark) */
  206.                                 if ( (UseDragBar && (EyesMenu->FirstItem->Flags & CHECKED))
  207.                                      || ( (!UseDragBar) && (!(EyesMenu->FirstItem->Flags & CHECKED))))
  208.                                 {
  209.                                   ToggleLockStat=TRUE;
  210.                                   watch=FALSE;
  211.                                   if (UseDragBar==0)
  212.                                   {
  213.                                     UseDragBar=WFLG_DRAGBAR;
  214.                                   }
  215.                                   else
  216.                                   {
  217.                                     YOffset=EyesWindow->TopEdge;
  218.                                     UseDragBar=0;
  219.                                   }
  220.                                   XOffset=EyesWindow->LeftEdge;
  221.                                 }
  222.                                 break;
  223.                               case 1:   /* About */
  224.                                 ShowRequester(  "Pointer Eyes v1.1  Copyright © 1994 John Hughes.\n"
  225.                                                 "All Rights Reserved.\n\n"
  226.                                                 "Written By:  John Hughes\n"
  227.                                                 "Date:        July 12,1994\n\n"
  228.                                                 "E-Mail:      johughes@heartland.bradley.edu\n");
  229.                                 break;
  230.                               case 2:   /* Quit */
  231.                                 watch=FALSE;
  232.                                 LOOP=FALSE;
  233.                                 break;
  234.                             }
  235.                           MyMessage->Code = menuitemaddress->NextSelect;
  236.                         }
  237.                         break;
  238.                     }
  239.                     GT_ReplyIMsg(MyMessage);
  240.                   }
  241.  
  242.                   /* If the window is going to be closed and or reopened set the priority back up */
  243.                   if (!watch)
  244.                     SetTaskPri(FindTask(NULL),OldPriority);
  245.  
  246.                   CurrentImage = GetImageNumber();
  247.                   ShowImage();
  248.                   Delay(3);
  249.                   if (CurCoOrd == PrevCoOrd)
  250.                     SameImage++;
  251.                   else
  252.                   {
  253.                     PrevCoOrd = CurCoOrd;
  254.                     SameImage = 0;
  255.                   }
  256.                   if(SameImage == 200)
  257.                   {
  258.                     GoToSleep();
  259.                     SameImage = 0;
  260.                   }
  261.                   if ((rand() % 1000)>993)
  262.                     BlinkEyes();
  263.  
  264.                 }  /* End of while(watch) */
  265.  
  266.                 /* Clear out all of the waiting messages */
  267.                 do {
  268.                 if (MyMessage = GT_GetIMsg(EyesWindow->UserPort))
  269.                    GT_ReplyIMsg(MyMessage);
  270.                 } while (MyMessage);
  271.  
  272.                 ClearMenuStrip( EyesWindow );
  273.               }
  274.               FreeMenus( EyesMenu );
  275.             }
  276.           }
  277.           FreeVisualInfo(EyesVisualInfo);
  278.         }
  279.         CloseWindow(EyesWindow);
  280.       }
  281.       else
  282.       {
  283.         ShowRequester("Couldn't Open Window.\n");
  284.         LOOP=FALSE;
  285.       }
  286.       if (ToggleLockStat)
  287.         ToggleLockStat=FALSE;
  288.       else if (LOOP)
  289.         Delay(100); /* Allow preferences time to close/open screen if need be */
  290.     }
  291.  
  292.     /* Stop watching the prefs drawer */
  293.     RemoveReqNotify();
  294.   }
  295.   else
  296.   {
  297.     filehandle=Open("CON:0/10/300/40/Pointer Eyes",MODE_NEWFILE);
  298.     Write(filehandle,"You must be running AmigaDOS 2.04 or higher.\n",45);
  299.     Delay(50*5);
  300.     Close(filehandle);
  301.     return(5);
  302.   }
  303. }
  304.  
  305.  
  306. /*****************************************************************/
  307.  
  308. int GetImageNumber(void)
  309. {
  310.   WORD   X, Y;
  311.   float  Angle;
  312.  
  313.  
  314.   X = (EyesWindow->MouseX)-20; /* bring it to...                (40 div 2 = 20) */
  315.   Y = (EyesWindow->MouseY)-4;  /* ...the center of the window.  (8 div 2 = 4)   */
  316.  
  317.   if (X==0)   /* We don't want to divide by 0! */
  318.     X=1;
  319.   Angle=atan((double)(Y/X));/* get Anglele in radians */
  320.   Angle=(Angle*180/3.14);   /* convert it to degrees */
  321.   if (Y==0)                 /* Check X-axis, since if Y=0, then Y/X=0 and always atan(0)=0 degrees */
  322.     Angle=(X>0) ? 0 : 180;
  323.   if ((X>0) && (Y>0))       /* Second quadrant */       // IV | I
  324.     Angle=360-Angle;                                    //    |
  325.   else if ((X<0) && (Y>0))  /* Third quadrant */        // ---+---
  326.     Angle=abs(Angle)+180;                               //    |
  327.   else if ((X<0) && (Y<0))  /* Fourth quadrant */       // III| II
  328.     Angle=180-Angle;
  329.   else if ((X>0) && (Y<0))  /* First quadrant */
  330.     Angle=abs(Angle);
  331.  
  332.   /* now we'll decide which image to show */
  333.   CurCoOrd = X + Y; 
  334.   if ((abs(X)<10) && (abs(Y)<4))      /* Cross Eyed */
  335.     return(0);  
  336.   if ((Angle>=22) && (Angle<68))      /* Upper Right */
  337.     return(1);
  338.   if ((Angle>=68) && (Angle<112))     /* Up */
  339.     return(3);
  340.   if ((Angle>=112) && (Angle<158))    /* Upper Left */
  341.     return(2);
  342.   if ((Angle>=158) && (Angle<202))    /* Left */
  343.     return(4);
  344.   if ((Angle>=202) && (Angle<247))    /* Lower Left */
  345.     return(5);
  346.   if ((Angle>=247) && (Angle<292))    /* Down */
  347.     return(6);
  348.   if ((Angle>=292) && (Angle<338))    /* Lower Right */
  349.     return(7);
  350.   if ((Angle>=338) || (Angle<22))     /* Right */
  351.     return(8);
  352.  
  353.   return(8);
  354. }
  355.  
  356. void ShowImage(void)
  357. {
  358.   /* Place the image of the eyes in the window */
  359.  
  360.   switch(CurrentImage)
  361.   {
  362.    case 0: DrawImage(Rast,&Crossed, 0, 0); break;
  363.    case 1: DrawImage(Rast,&RU, 0, 0); break;
  364.    case 2: DrawImage(Rast,&LU, 0, 0); break;
  365.    case 3: DrawImage(Rast,&Up, 0, 0); break;
  366.    case 4: DrawImage(Rast,&Left, 0, 0); break;
  367.    case 5: DrawImage(Rast,&LD, 0, 0); break;
  368.    case 6: DrawImage(Rast,&Down, 0, 0); break;
  369.    case 7: DrawImage(Rast,&RD, 0, 0); break;
  370.    case 8: DrawImage(Rast,&Right, 0, 0); break;
  371.   }
  372. }
  373.  
  374. void BlinkEyes(void)
  375. {
  376.   DrawImage(Rast,&NoEyes, 0, 0);
  377.   Delay(1);
  378.   DrawImage(Rast,&TopBlink, 0, 0);
  379.   Delay(1);
  380.   DrawImage(Rast,&MidBlink, 0, 0);
  381.   Delay(1);
  382.   DrawImage(Rast,&BotBlink, 0, 0);
  383.   Delay(1);
  384.   DrawImage(Rast,&NoEyes, 0, 0);
  385.   Delay(12);
  386.   DrawImage(Rast,&BotBlink, 0, 0);
  387.   Delay(1);
  388.   DrawImage(Rast,&MidBlink, 0, 0);
  389.   Delay(1);
  390.   DrawImage(Rast,&TopBlink, 0, 0);
  391.   Delay(1);
  392.   DrawImage(Rast,&NoEyes, 0, 0);
  393.   Delay(3);
  394. }
  395.  
  396. void LookAround(void)
  397. {
  398.   /* Make the eyes look right then left. */
  399.  
  400.   int count;
  401.  
  402.   for (count=0; count<2; count++)
  403.   {
  404.     DrawImage(Rast,&Straight, 0, 0);
  405.     Delay(3);
  406.     DrawImage(Rast,&Right, 0, 0);
  407.     Delay(30);
  408.     DrawImage(Rast,&Straight, 0, 0);
  409.     Delay(6);
  410.     DrawImage(Rast,&Left, 0, 0);
  411.     Delay(30);
  412.     DrawImage(Rast,&Straight, 0, 0);
  413.     Delay(3);
  414.   }
  415. }
  416.  
  417. void CloseEyes(void)
  418. {
  419.   /* Make eyes shut */
  420.  
  421.   DrawImage(Rast,&NoEyes, 0, 0);
  422.   Delay(2);
  423.   DrawImage(Rast,&TopBlink, 0, 0);
  424.   Delay(2);
  425.   DrawImage(Rast,&MidBlink, 0, 0);
  426.   Delay(2);
  427.   DrawImage(Rast,&BotBlink, 0, 0);
  428.   Delay(10);
  429. }
  430.  
  431. void GoToSleep(void)
  432. {
  433.   SameImage = 0;
  434.   LookAround();
  435.   Delay(10);
  436.   CloseEyes();
  437.   CurrentImage = GetImageNumber();
  438.   while (CurCoOrd == PrevCoOrd)
  439.   {
  440.     Delay(10);
  441.     CurrentImage = GetImageNumber();
  442.     SameImage++;
  443.     if (SameImage == 150)
  444.     {
  445.       LookAround();
  446.       SameImage = 0;
  447.       ShowImage();
  448.       Delay(25);
  449.       CloseEyes();
  450.     }
  451.   }
  452.   DrawImage(Rast,&MidBlink, 0, 0);
  453.   Delay(2);
  454.   DrawImage(Rast,&TopBlink, 0, 0);
  455.   Delay(2);
  456.   DrawImage(Rast,&NoEyes, 0, 0);
  457.   Delay(2);
  458. }
  459.  
  460.  
  461.  
  462. BOOL CreateReqNotify(UBYTE *FileOrDir)
  463. {
  464.   /* Create a notification for a file or directory */
  465.   /* Returns TRUE if successful */
  466.  
  467.   BOOL success=FALSE;
  468.  
  469.   RemoveReqNotify();
  470.  
  471.   if (notifyrequest=AllocMem(sizeof(struct NotifyRequest),MEMF_CLEAR))
  472.   {
  473.     if ((reqnotifysignum=AllocSignal(-1L))!=-1)
  474.     {
  475.       startnotsigflag=1L<<reqnotifysignum;
  476.       notifyrequest->nr_Name=FileOrDir;
  477.       notifyrequest->nr_Flags=NRF_SEND_SIGNAL;
  478.       notifyrequest->nr_stuff.nr_Signal.nr_Task=(struct Task *) FindTask(NULL);
  479.       notifyrequest->nr_stuff.nr_Signal.nr_SignalNum=reqnotifysignum;
  480.       if ((StartNotify(notifyrequest))==DOSTRUE)
  481.         success=TRUE;
  482.       else
  483.       {
  484.         ShowRequester("Can't start notification.\n");
  485.         FreeSignal(reqnotifysignum);
  486.       }
  487.     }
  488.     else
  489.     {
  490.       /* No signals available */
  491.       FreeMem(notifyrequest,sizeof(struct NotifyRequest));
  492.     }
  493.   }
  494.   else
  495.     ShowRequester("Not enough memory for NotifyRequest.\n");
  496.  
  497.   return(success);
  498. }
  499.  
  500. void RemoveReqNotify(void)
  501. {
  502.   /* Removes a notification, if one was created */
  503.  
  504.   if (reqnotifysignum)
  505.   {
  506.     EndNotify(notifyrequest);
  507.     FreeSignal(reqnotifysignum);
  508.     reqnotifysignum=NULL;
  509.     FreeMem(notifyrequest,sizeof(struct NotifyRequest));
  510.   }
  511. }
  512.  
  513. void ShowRequester(STRPTR RequesterText)
  514. {
  515.   struct EasyStruct myRequestStruct={
  516.     sizeof (struct EasyStruct ),
  517.   0,
  518.   "Pointer Eyes",
  519.   NULL,
  520.   "Ok"};
  521.  
  522.   myRequestStruct.es_TextFormat=RequesterText;
  523.   EasyRequestArgs(EyesWindow,&myRequestStruct,NULL,NULL);
  524. }